package com.atguigu.eduservice.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.mapper.EduTeacherMapper;
import com.atguigu.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-10-16
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {

    @Autowired
    private RedisTemplate redisTemplate;

    //注入分布式锁
    @Autowired
    private RedissonClient redissonClient;

    @Override
    public Map<String, Object> getTeacherFrontList(Page<EduTeacher> pageParam) {
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        //把分页数据封装到pageTeacher对象
        baseMapper.selectPage(pageParam,wrapper);

        List<EduTeacher> records = pageParam.getRecords();
        long current = pageParam.getCurrent();
        long pages = pageParam.getPages();
        long size = pageParam.getSize();
        long total = pageParam.getTotal();
        boolean hasNext = pageParam.hasNext();//下一页
        boolean hasPrevious = pageParam.hasPrevious();//上一页

        //把分页数据获取出来，放到map集合
        Map<String, Object> map = new HashMap<>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        //map返回
        return map;
    }

    @Override
    public List<EduTeacher> getteacherlist() {


        String teachers = (String) redisTemplate.opsForValue().get("teachers");

        if (StringUtils.isEmpty(teachers)){

            RLock tlock = redissonClient.getLock("tlock");

            tlock.lock(30, TimeUnit.SECONDS);

            QueryWrapper<EduTeacher> teacherWrapper=new QueryWrapper<>();
            teacherWrapper.orderByDesc("id");
            teacherWrapper.last("limit 4");
            List<EduTeacher> teacherList = baseMapper.selectList(teacherWrapper);

            String s = JSON.toJSONString(teacherList);

            redisTemplate.opsForValue().set("teachers",s,1,TimeUnit.DAYS);

            tlock.unlock();

            return teacherList;


        }

        List<EduTeacher> teacherList = JSON.parseObject(teachers, new TypeReference<List<EduTeacher>>(){});

        return teacherList;

    }

}

